package com.example.fw;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 13.03.13
 * Time: 16:39
 * To change this template use File | Settings | File Templates.
 */
public abstract class RandomHelper {

    private static Random rnd;
    private static List<String> monthList;

    private static String generateRandomString(String characters, int length)
    {
        String resultString = "";
        int charCount = characters.length();
        int charNumber;

        for (int i = 0; i < length; i++)
        {
            resultString += characters.substring(charNumber = getRnd().nextInt(charCount-1), ++charNumber);
        }

        return resultString;
    }

    public static Random getRnd() {
        if (rnd == null)
            rnd = new Random();
        return rnd;
    }

    public static List<String> getMonthList() {
        if (monthList == null)
            monthList = new LinkedList<String>(){
                {
                    add("-");
                    add("January");
                    add("February");
                    add("March");
                    add("April");
                    add("May");
                    add("June");
                    add("July");
                    add("August");
                    add("September");
                    add("October");
                    add("November");
                    add("December");
                }
            };
        return monthList;
    }

    public static String getNextIntDropdown(int valuesCount){
        int result = getRnd().nextInt(valuesCount + 1);
        if (result == 0)
            return "-";
        else
            return "" + result;
    }

    public static String getNullable(String input, int nullFreq){
        if (getRnd().nextInt(nullFreq) == 0)
            return null;
        else return input;
    }

    public static String getRandomStringValue(int maxLength) {
        //generateRandomString("!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", getRnd().nextInt(maxLength));
        // с этой строкой валится всё
        String result = generateRandomString("- 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", getRnd().nextInt(maxLength));
        if (result.length() == 0)
            return result;
        result = result.trim();
        //Убрать пробелы и дефисы из начала и конца
        if (result.indexOf('-') == 0)
            result = generateRandomString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 1) + result;
        if (result.indexOf('-') == result.length() - 1)
            result = result + generateRandomString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 1);

        return result;

    }

    public static String unNullify(String input){
        if (input == null)
            return "";
        else
            return input;
    }
}
